package com.fizzkai.p2p.website.controller;

import com.fizzkai.p2p.bussness.domain.PaymentSchedule;
import com.fizzkai.p2p.bussness.query.PaymentScheduleQuery;
import com.fizzkai.p2p.bussness.service.IPaymentScheduleService;
import com.fizzkai.p2p.core.domain.Account;
import com.fizzkai.p2p.core.service.IAccountService;
import com.fizzkai.p2p.core.util.AjaxResult;
import com.fizzkai.p2p.core.util.UserContext;
import com.fizzkai.p2p.website.util.RequireLogin;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ReturnMoneyController {

    @Autowired
    private IPaymentScheduleService paymentScheduleService;
    @Autowired
    private IAccountService accountService;

    @RequestMapping("/borrowBidReturn_list")
    @RequireLogin
    public String borrowBidReturn_list(@ModelAttribute("qo") PaymentScheduleQuery qo , Integer currentPage, Model model){
        if (currentPage != null) {
            qo.setPageNum(currentPage);
        }
        qo.setBorrowUserId(UserContext.getCurrentLoginInfo().getId());
        PageInfo<PaymentSchedule> pageResult = paymentScheduleService.query(qo);
        Account account = accountService.getCurrent();

        model.addAttribute("pageResult", pageResult);
        model.addAttribute("account", account);
        return "returnmoney_list";
    }

    /**
     * 还款 money
     * @param id
     * @return
     */
    @RequestMapping("/returnMoney")
    @ResponseBody
    @RequireLogin
    public Object returnMoney(Long id){
        paymentScheduleService.returnMoney(id);
        return new AjaxResult("还款成功");
    }
}
